package util;

import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author 嫣然
 */
public class DataBaseConnection {
    private static Connection conn=null;
    private static final String URL="jdbc:mysql://localhost:3306/test";
    private static final String USER="root";
    private static final String PASSWORD="123456";
    private static final String DRIVER="com.mysql.jdbc.Driver";

    /**
     * 连接数据库
     * @return
     */
    public static Connection getConnection(){
        try {
            Class.forName(DRIVER);
            conn= DriverManager.getConnection(URL,USER,PASSWORD);
        }catch (Exception e){
            e.printStackTrace();
        }
        return conn;
    }
    /**
     * 关闭资源
     * @param conn
     * @param pst
     * @param rs
     * @throws Exception
     */
    public static void close(Connection conn, PreparedStatement pst, ResultSet rs) throws Exception{
        if (rs!=null){
            rs.close();
        }
        if (pst!=null){
            pst.close();
        }
        if (conn!=null){
            conn.close();
        }
    }
}
